Completed
Push — master ( 73fdbe...e1a05e )
by Jean
01:06
created

configure.js ➔ load   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 24
rs 8.9713

1 Function

Rating   Name   Duplication   Size   Complexity  
A configure.js ➔ ... ➔ ??? 0 22 2
1
/**
2
 * @file Module 'configure'
3
 * @author woshilapin <[email protected]>
4
 * @version 0.3.0
5
 */
6
/**
7
 * Manage global configuration for Codingame's connector
8
 * @module configure
9
 */
10
import fs from 'fs';
11
import readline from 'readline';
12
import subprocess from 'child_process';
13
14
let parameters = {};
15
16
const rl = readline.createInterface({
17
	"input": process.stdin,
18
	"output": process.stdout
19
});
20
21
22
/**
23
 * Load the configuration file
24
 *
25
 * @name load
26
 * @function
27
 * @param {string} path Path of the configuration file
28
 * @param {Object} [options] Additionnal parameters which will replace parameters from configuration file
0 ignored issues
show
Documentation Bug introduced by
The parameter [options] does not exist. Did you maybe mean options instead?
Loading history...
29
 * @returns {Promise<Object>} Configuration parameters
30
 * @memberof module:configure
31
 * @instance
32
 */
33
let load = function load(path, options) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable load already seems to be declared on line 33. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
34
	return new Promise(function(resolve, reject) {
35
		let resolvefromerror = function resolvefromerror(error) {
36
			if (options !== undefined && typeof options === `object`) {
37
				resolve(options);
38
			} else {
39
				reject(error);
40
			}
41
		};
42
		try {
43
			fs.readFile(path, `utf8`, function(error, file) {
44
				if (error) {
45
					resolvefromerror(error);
46
				} else {
47
					parameters = JSON.parse(file);
48
					Object.assign(parameters, options);
49
					resolve(parameters);
50
				}
51
			});
52
		} catch (error) {
53
			resolvefromerror(error);
54
		}
55
	});
56
};
57
58
/**
59
 * Get the value from configuration
60
 *
61
 * @name get
62
 * @function
63
 * @param {string} name Name of the parameter
64
 * @param {string} [option] 'shell' if value may be executed as shell command, 'file' if value is a path and content of file should be returned
0 ignored issues
show
Documentation Bug introduced by
The parameter [option] does not exist. Did you maybe mean option instead?
Loading history...
65
 * @param {string} [question] If present and value not found, will be shown to the user to ask for the value on 'stdin'
0 ignored issues
show
Documentation Bug introduced by
The parameter [question] does not exist. Did you maybe mean question instead?
Loading history...
66
 * @returns {Promise<string|Array|Object>} The value of the parameter
67
 * @memberof module:configure
68
 * @instance
69
 */
70
let get = function get(name, option, question) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable get already seems to be declared on line 70. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
71
	return new Promise(function(resolve, reject) {
72
		if (!name || typeof name !== `string`) {
73
			reject(new Error(`'configure.get()' takes at least one string parameter.`));
74
		}
75
		let property = parameters[name];
76
		if (property !== undefined && option !== undefined && option === `shell` && Array.isArray(property)) {
77
			subprocess.exec(property.join(` `), function(error, stdout, stderr) {
78
				if (error) {
79
					console.error(stderr);
80
					reject(error);
81
				} else {
82
					let result = stdout.trim();
83
					parameters[name] = result;
84
					resolve(result);
85
				}
86
			});
87
		} else if (property !== undefined && option !== undefined && option === `file`) {
88
			fs.readFile(property, `utf8`, function(error, file) {
89
				if (error) {
90
					reject(error);
91
				} else {
92
					resolve({
93
						"path": property,
94
						"content": file
95
					});
96
				}
97
			});
98
		} else if (property !== undefined) {
99
			resolve(property);
100
		} else {
101
			if (question !== undefined && typeof question === `string`) {
102
				rl.question(question, (result) => {
103
					parameters[name] = result;
104
					resolve(result);
105
				});
106
			} else {
107
				reject(new Error(`'configure.get()' second parameter must be a string.`));
108
			}
109
		}
110
	});
111
};
112
113
/**
114
 * Ask configure module to delete a parameter
115
 *
116
 * @name forget
117
 * @function
118
 * @param {string} name Name of the parameter
119
 * @memberof module:configure
120
 * @instance
121
 */
122
let forget = function forget(name) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable forget already seems to be declared on line 122. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
123
	if (!name || typeof name !== `string`) {
124
		throw new Error(`'configure.forget()' function takes one string parameter.`);
125
	} else {
126
		delete parameters[name];
127
	}
128
};
129
130
export default {
131
	"load": load,
132
	"get": get,
133
	"forget": forget
134
};
135